﻿using System;
using System.Collections.Generic;
using System.Linq;

using PortalIntranet.DatabaseComponents;
using PortalIntranet.DatabaseComponents.WebParts.Galerias;
using PortalIntranet.ObjectComponents.WebParts.Galerias;
using PortalIntranet.DatabaseComponents.WebParts;
using PortalIntranet.BusinessComponents.WebParts.Galerias;

namespace PortalIntranet.BusinessComponents.WebParts.LinksGerais
{
	public class GaleriasBll
	{
		public static Galeria Obter(int id)
		{
			using (var contexto = new IntranetContexto())
			{
				return contexto.Galerias
					.Where(c => c.Id == id
						&& !c.Desativado
						&& !c.Excluido)
					.SingleOrDefault();
			}
		}

		public static Galeria ObterPorWebPart(int webpart)
		{
			using (var contexto = new IntranetContexto())
			{
				return contexto.Galerias
					.Where(c => c.WebPartId == webpart
						&& !c.Desativado
						&& !c.Excluido)
					.SingleOrDefault();
			}
		}

		public static WPGaleria ConverterWebPart(WebPart webpart)
		{
			Galeria galeria = ObterPorWebPart(webpart.Id);
			List<GaleriaItem> itens = GaleriasItensBll.ListarGaleriasItens(galeria.Id);
			WPGaleria wpgaleria = new WPGaleria(webpart);

			wpgaleria.Nome = galeria.Nome;
			wpgaleria.Itens = new List<GaleriaItemInfo>();

			foreach (var item in itens)
			{
				wpgaleria.Itens.Add(new GaleriaItemInfo(item));
			}

			return wpgaleria;
		}
	}
}
